//
//  AppDelegate.m
//  A One POS
//
//  Created by AonePOS on 24/09/13.
//  Copyright (c) 2013 AonePOS. All rights reserved.

#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate

@synthesize viewController,navController,window,admindataArray;
//@synthesize profileObj,contactsObj,storesObj,mainViewObj,settingsObj,inventoryObj,customersObj,employeesObj,reportsObj,homeObj;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // [self createiMagReader];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    //admindataArray=[[NSMutableArray alloc]init];
    viewController = [[ViewController alloc] init];
    navController = [[UINavigationController alloc] initWithRootViewController:viewController];
   
    
     self.window.rootViewController = navController;
    navController.navigationBarHidden=YES;

    [self.window makeKeyAndVisible];
    
    
   
      return YES;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
{
    return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate  // iOS 6 autorotation fix
{
    return YES;
}

/*-(void)createiMagReader

{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iMagAccessoryConnected:) name:@"iMagDidConnectNotification" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iMagAccessoryDisconnected:) name:@"iMagDidDisconnectNotification" object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iMagAccessoryData:) name:@"iMagDidReceiveDataNotification" object:nil];
	
	
	//iReader = [[iMag alloc] init];
    
}

- (void)iMagAccessoryConnected:(NSNotification *)notification{
    self.iMagStatusStr = @"Connected";
    
    UIAlertView *alert=[[UIAlertView alloc
                         ]initWithTitle:@"card" message:@"connected" delegate:self cancelButtonTitle:Nil otherButtonTitles:@"ok", nil ];
    [alert show];
}

- (void)iMagAccessoryDisconnected:(NSNotification *)notification
{
    self.iMagStatusStr = @"Not Connected";
    
}

- (void)iMagAccessoryData:(NSNotification *)notification
{
	//This observer is called when data comes in from the reader; NSData Object is received
    
	NSData *data = [notification object];
    
	NSString *baseStrng = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    trackString=baseStrng;
    NSString *matchString= @";";
    NSRange range = [trackString rangeOfString:matchString];
    NSInteger idx = range.location + range.length;
    track1=[trackString substringWithRange:NSMakeRange(0, range.location)];
    track2 =[trackString substringFromIndex:idx];
    [self scanAccessoryData:baseStrng];
    
}
-(void)scanAccessoryData:(NSString *)baseStr
{
    NSString*accountNumber1;
    NSString*cardHolderName1;
    NSString*expdate1;
    cardType = @"";
    cardHolderName =@"";
    accountNumber = @"";
    expdate = @"";
    baseString = baseStr;
	cardDataScanner = [NSScanner scannerWithString:baseString];
    
	while ([cardDataScanner isAtEnd] == NO) {
		if (([baseString rangeOfString:@"%"].location == NSNotFound ) ||([baseString rangeOfString:@"^"].location == NSNotFound )) {
		}
		else {
			[cardDataScanner scanUpToString:@"%" intoString:NULL];
			[cardDataScanner setScanLocation:[cardDataScanner scanLocation]+2];
			[cardDataScanner scanUpToString:@"^" intoString:&accountNumber1];
            [cardDataScanner scanUpToString:@"^" intoString:NULL];
			[cardDataScanner setScanLocation:[cardDataScanner scanLocation]+1];
			[cardDataScanner scanUpToString:@"^" intoString:&cardHolderName1];
            [cardDataScanner scanUpToString:@"^" intoString:NULL];
            [cardDataScanner setScanLocation:[cardDataScanner scanLocation]+1];
            [cardDataScanner scanUpToString:@"?" intoString:&expdate1];
            expdate = [expdate1 substringToIndex:4];
            expdate = [NSString stringWithFormat:@"%@/%@",[expdate1 substringToIndex:2],[expdate1 substringFromIndex:2]];
            accountNumber=accountNumber1;
            cardHolderName=cardHolderName1;
            
            
            if(isFromCreditCardDetails==NO)
                NSLog(@"exp date %@ ",expdate)	;
        }
		break;
	}
    if([cardHolderName hasSuffix:@"/"])
    {
        cardHolderName = [cardHolderName substringToIndex:[cardHolderName length] - 1];
    }
    if([cardHolderName isEqualToString:@"CARD HOLDER"]){
        cardHolderName = @"";
    }
    if(isFromCreditCardDetails==NO)
    {
        
        //chinni
        
        UIAlertView *alert=[[UIAlertView alloc
                             ]initWithTitle:@"card details" message:[NSString stringWithFormat:@" holder name %@,card number %@,exp date %@",cardHolderName,accountNumber,expdate] delegate:self cancelButtonTitle:Nil otherButtonTitles:@"ok", nil ];
        [alert show];
        
        NSLog(@"card name%@",cardHolderName);
        generalOrderHeader.cardName=cardHolderName;
         [generalOrderHeader setValue:cardHolderName forKey:@"cardName"];
         generalOrderHeader.cardNumber=accountNumber;
         [generalOrderHeader setValue:accountNumber forKey:@"cardNumber"];
    }
    //Rajeethulla
    //  depositsCreditModalDict=[[NSMutableDictionary alloc] init];
    //[depositsCreditModalDict setObject:accountNumber forKey:@"CardNumber"];
    cardHolderName=[cardHolderName stringByReplacingOccurrencesOfString:@"*" withString:@""];
    cardHolderName=[cardHolderName stringByReplacingOccurrencesOfString:@" " withString:@""];
    //[depositsCreditModalDict setObject:cardHolderName forKey:@"FirstName"];
    //[depositsCreditModalDict setObject:expdate forKey:@"CardExpiryYear"];
    NSString *cardYear= [NSString stringWithFormat:@"%@",expdate];
    
    //    NSLog(@"cardYear--->%@",cardYear);
    
    NSString *cardexpyy=[cardYear substringWithRange:NSMakeRange(0, 2)];
    NSString *cardexpmm=[cardYear substringWithRange:NSMakeRange(3, 2)];
    if (cardexpmm!=NULL && cardexpyy!=NULL) {
        
        //chinni
        
        UIAlertView *alert=[[UIAlertView alloc
                             ]initWithTitle:@"card details" message:[NSString stringWithFormat:@" cardexpmm %@,cardexpyy %@,exp date %@",cardexpmm,[NSString stringWithFormat:@"20%@",cardexpyy],expdate] delegate:self cancelButtonTitle:Nil otherButtonTitles:@"ok", nil ];
        [alert show];
        //[depositsCreditModalDict setObject:cardexpmm forKey:@"CardExpiryMonth"];
        //[depositsCreditModalDict setObject:[NSString stringWithFormat:@"20%@",cardexpyy] forKey:@"CardExpiryYear"];
    }
    [self CheckCardType:accountNumber];
    // by madhav....
    
    //chinni
    UIAlertView *alert=[[UIAlertView alloc
                         ]initWithTitle:@"card details" message:[NSString stringWithFormat:@" cardHolderName %@,accountNumber %@,cardType %@ mm=%@,year=%@",cardHolderName,accountNumber,cardType,cardexpmm,[NSString stringWithFormat:@"20%@",cardexpyy]] delegate:self cancelButtonTitle:Nil otherButtonTitles:@"ok", nil ];
    [alert show];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CreditCardSwipe" object:nil userInfo:nil ];
    //[[NSNotificationCenter defaultCenter]postNotificationName:@"CreditCardSwipe" object:[creditCard retain]];
    
    CardDataDict=[[NSMutableDictionary alloc]initWithObjectsAndKeys:cardHolderName,@"Holdername",accountNumber,@"accountNumber",cardexpyy,@"ExpYear",cardexpmm,@"ExpMonth",@"",cardType,@"CrdType", nil];
    
    
    
    
}*/

/*-(void)CheckCardType:(NSString *)AccountNumber
{
	if([AccountNumber hasPrefix:@"4"]&&((AccountNumber.length ==13)||(AccountNumber.length ==16)))
	{
        cardType = @"VISA";
	}
	if((([AccountNumber hasPrefix:@"34"])||([AccountNumber hasPrefix:@"35"])||([AccountNumber hasPrefix:@"36"])||([AccountNumber hasPrefix:@"37"]))&&(AccountNumber.length ==15))
	{
        cardType = @"American Express";
	}
	if((([AccountNumber hasPrefix:@"51"])||([AccountNumber hasPrefix:@"52"])||([AccountNumber hasPrefix:@"53"])||([AccountNumber hasPrefix:@"54"])||([AccountNumber hasPrefix:@"55"]))&&(AccountNumber.length ==16))
	{
        cardType = @"Master Card";
	}
	if(([AccountNumber hasPrefix:@"6011"])&&(AccountNumber.length ==16))
	{
		cardType = @"Discover Card";
	}
    if(isFromCreditCardDetails==NO)
    {
        
        //chinni
        UIAlertView *alert=[[UIAlertView alloc
                             ]initWithTitle:@"card type" message:[NSString stringWithFormat:@"cardType%@ ",cardType] delegate:self cancelButtonTitle:Nil otherButtonTitles:@"ok", nil ];
        [alert show];
        
    }
    
    
    if (isFromCreditCardDetails==NO)
    {
        
        //  [self playToneOnSuccessfulSwipe];
        
    }
    if(isFromCreditCardDetails==NO)
    {
        
    }
    else
    {
        // [self playToneOnSuccessfulSwipe];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadAfterScan" object:nil];
    }
}
- (BOOL) isConnected
{
	//connection status of external accessory
    
	return iReader.iMagConnected;
}*/

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
